//
//  LRDatabaseManager.m
//  Anyair
//
//  Created by karlcool on 14-5-25.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "LRDatabaseManager.h"
static LRDatabaseManager *manager;

@implementation LRDatabaseManager

+(LRDatabaseManager *)createDatabase{
    
    @synchronized(self)
    {
        if (manager == nil) {
            manager=[[self alloc] init];
        }
    }
    return manager;
}

-(id)init{
    
    self=[super init];
    
    if(self){
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *docsPath = [paths objectAtIndex:0];
        NSString *realPath = [docsPath stringByAppendingPathComponent:@"Anyair.sqlite"];
        NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"Anyair" ofType:@"sqlite"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if (![fileManager fileExistsAtPath:realPath]) {
            NSError *error;
            if (![fileManager copyItemAtPath:sourcePath toPath:realPath error:&error]) {
                NSLog(@"%@",[error localizedDescription]);
            }
        }
        self.database = [FMDatabase databaseWithPath:realPath];
        self.dataQueue=[FMDatabaseQueue databaseQueueWithPath:realPath];
        NSLog(@"数据库地址%@",realPath);
    }
    return  self;
}

#pragma mark - 获取数据库中未上传的数据
- (NSMutableArray*)getUpdateArray {
    
    __block NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    
    [manager.dataQueue inDatabase:^(FMDatabase *db) {
        [db open];
        //UpdateSuccess为成功标示符，上传成功置为2，上传第一次失败置为1，上传第二次失败置为2，置为2就将被清除
        FMResultSet *set = [db executeQuery:@"select * from UpdateTable where UpdateSuccess = 0 or UpdateSuccess = 1"];
        while ([set next]) {
            UpdateModel *tempModel = [[UpdateModel alloc]init];
            tempModel.updateID = [set intForColumn:@"UpdateID"];
            tempModel.updatePhone = [set stringForColumn:@"UpdatePhone"];
            tempModel.updateLongitude = [set stringForColumn:@"UpdateLongitude"];
            tempModel.updateLatitude = [set stringForColumn:@"UpdateLatitude"];
            tempModel.updateAddress = [set stringForColumn:@"UpdateAddress"];
            tempModel.updateCelsius = [set stringForColumn:@"UpdateCelsius"];
            tempModel.updateDampness = [set stringForColumn:@"UpdateDampness"];
            tempModel.updateTime = [set stringForColumn:@"UpdateTime"];
            tempModel.updatePlacediy = [set stringForColumn:@"UpdatePlacediy"];
            tempModel.updateCO2 = [set stringForColumn:@"UpdateCO2"];
            tempModel.updateHOCO = [set stringForColumn:@"UpdateHOCO"];
            tempModel.updatePM25 = [set stringForColumn:@"UpdatePM25"];
            tempModel.updatePM50 = [set stringForColumn:@"UpdatePM50"];
            tempModel.updatePM100 = [set stringForColumn:@"UpdatePM100"];
            tempModel.updateType = [set stringForColumn:@"UpdateType"];
            [dataArray addObject:tempModel];
        }
 
        [db close];
    }];
    
    return dataArray;
}

#pragma mark - 上传成功清除数据
- (void)deleteUpdateModelByUpdateId:(int)updateId {
    
    [manager.dataQueue inDatabase:^(FMDatabase *db) {
        [db open];
        
        [db executeUpdate:@"delete from UpdateTable where UpdateID = ?",[NSNumber numberWithInt:updateId]];
        
        [db close];
    }];
    
}
#pragma mark - 上传失败做标记下次再上传一次，再失败就清除
- (void)setUpdateModelFailByUpdateId:(int)updateId {
    [manager.dataQueue inDatabase:^(FMDatabase *db) {
        [db open];
        
        [db executeUpdate:@"update UpdateTable set UpdateSuccess = 1 from  where UpdateID = ?",[NSNumber numberWithInt:updateId]];
        
        [db close];
    }];
}


#pragma mark - 获取对应电话和类型的历史数据

- (NSMutableArray*)getHistoryArrayByPhone:(NSString*)phone AndType:(int)type {
    
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    [manager.dataQueue inDatabase:^(FMDatabase *db) {
        [db open];
        
        FMResultSet *set = [db executeQuery:@"select * from HistoryTable where UserPhone = ? And HistoryType = ?",phone,[NSNumber numberWithInt:type]];
        
        while ([set next]) {
            
            HistoryModel *tempModel = [[HistoryModel alloc]init];
            tempModel.historyAddress = [set stringForColumn:@"HistoryAddress"];
            tempModel.historyValue = [set stringForColumn:@"HistoryValue"];
            tempModel.historyType = [NSString stringWithFormat:@"%d",type];
            tempModel.historyDate = [set stringForColumn:@"HistoryDate"];
            tempModel.userPhone = phone;
            [resultArray addObject:tempModel];
        }
        
        [db close];
    }];
    return resultArray;
}

//使用范例
//LRDatabaseManager *manager=[LRDatabaseManager createDatabase];
//[manager.dataQueue inDatabase:^(FMDatabase *db){
//    [db open];
//
//    [db close];
//}];


@end
